﻿namespace ChinaUnicom.ViewModels
{
    using ChinaUnicom.Models;
    using System;
    using System.ComponentModel;
    using System.Linq;

    public class FeeHistoryContext : PageContext, INotifyPropertyChanged
    {
        public FeeHistoryContext(int year, int month)
        {
            Occurred = new DateTime(year, month, 1);

            if (Context.Current.FeeHistories.Contains(Occurred))
            {
                var history = Context.Current.FeeHistories[occurred];
                Started = history.Started;
                Ended = history.Ended;
                Details = history.Items;
            }
        }

        DateTime occurred;
        public DateTime Occurred
        {
            get { return occurred; }
            set
            {
                if (value != occurred)
                {
                    occurred = value;
                    NotifyPropertyChanged("Occurred");
                }
            }
        }

        DateTime started;
        public DateTime Started
        {
            get { return started; }
            set
            {
                if (value != started)
                {
                    started = value;
                    NotifyPropertyChanged("Started");
                }
            }
        }

        DateTime ended;
        public DateTime Ended
        {
            get { return ended; }
            set
            {
                if (value != ended)
                {
                    ended = value;
                    NotifyPropertyChanged("Ended");
                }
            }
        }

        ItemCollection details;
        public ItemCollection Details
        {
            get { return details; }
            set
            {
                if (value != this.details)
                {
                    details = value;
                    NotifyPropertyChanged("Details");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
